package com.bawei.park2211a.repair

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.bean.AddRepairEntity
import com.bawei.base.mvvm.BaseActivity
import com.bawei.park2211a.R
import com.bawei.park2211a.databinding.ActivityAddRepairBinding
import com.blankj.utilcode.util.ToastUtils
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path = "/app/repair/AddRepairActivity")
class AddRepairActivity : BaseActivity<ActivityAddRepairBinding,AddRepairViewModel>() {
    var area="东区"
    override fun initView() {
        val arrayListOf = arrayListOf("东区", "西区", "南区", "北区", "中央区域")
        var adapter:ArrayAdapter<String> = ArrayAdapter(this, com.chad.library.R.layout.support_simple_spinner_dropdown_item,arrayListOf)
        binding.repairArea.adapter=adapter
        binding.repairArea.onItemSelectedListener=object :OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                area=arrayListOf[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {
            }
        }
        binding.repairSubmit.setOnClickListener {
            if (binding.repairMsg.text.isEmpty()){
                binding.repairMsg.error = "请输入报修信息"
                return@setOnClickListener
            }
            if (binding.repairName.text.isEmpty()){
                binding.repairName.error = "请输入姓名"
                return@setOnClickListener
            }
            if (binding.repairPhone.text.isEmpty()){
                binding.repairPhone.error = "请输入手机号"
                return@setOnClickListener
            }
            val addRepairEntity = AddRepairEntity()
            //获取当前时间
            val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            val currentTime = sdf.format(System.currentTimeMillis())
            addRepairEntity.faultDescription=binding.repairMsg.text.toString()
            addRepairEntity.repairName=binding.repairName.text.toString()
            addRepairEntity.repairTime=currentTime
            addRepairEntity.orderNumber="WX202503041011"
            viewModel.addRepair(addRepairEntity)



        }
    }

    override fun initData() {
        viewModel.addRepairSuccess.observe(this){
            ToastUtils.showLong("提交成功")
            ARouter.getInstance().build("/app/repair/RepairActivity").navigation()
        }
    }
}